package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.config.RedisService;
import com.zzyl.dto.ReservationDTO;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.ReservationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper  customerReservationMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public Integer getCancelledReservationCount(Long userId) {
        return customerReservationMapper.getCancelledReservationCount(
                LocalDateTime.now().withHour(0).withMinute(0).withSecond(0),
                LocalDateTime.now().withHour(23).withMinute(59).withSecond(59),
                userId);
    }

    @Override
    public void add(ReservationDTO reservationdto) {
        Long userId = UserThreadLocal.getUserId();
        if (getCancelledReservationCount(userId) >= 3){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }else {
            // 从 Redis 获取 time
            Long time = redisService.getTime(userId);
            if (time == null) {
                throw new RuntimeException("未找到保存的时间参数");
            }
            List<ReservationCountVo> reservationCountVos = countReservationsTimeRange(LocalDateTimeUtil.of(time));
            for (ReservationCountVo reservationCountVo : reservationCountVos) {
                if (reservationCountVo.getCount() <= 0) {
                    throw new BaseException(BasicEnum.ORDER_CLOSED);
                }
            }
            customerReservationMapper.insert(reservationdto);
        }
    }

    @Override
    public PageResponse<ReservationVO> page(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Reservation> nursingProjects = customerReservationMapper.selectByPage(status);
        PageResponse<ReservationVO> projectVoPageResponse = PageResponse.of(nursingProjects, ReservationVO.class);
        return projectVoPageResponse;
    }

    @Override
    public void cancel(Long id) {
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        customerReservationMapper.upstateById(reservation);
    }

    @Override
    public List<ReservationCountVo> countReservationsTimeRange(LocalDateTime of) {
        LocalDateTime startTime = of.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = of.withHour(23).withMinute(59).withSecond(59);
        List<ReservationCountVo> reservationVOS = customerReservationMapper.countReservationsTimeRange(startTime,endTime);
        return reservationVOS;
    }

    @Override
    public void expireOutdatedReservations() {
        Date now = new Date();
        // 调用Mapper方法，更新过期的预约状态
        customerReservationMapper.updateExpiredReservations(now);
    }
}
